package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Cargo;

public class CargoDaoImplementation implements CargoDao {

	public void save(Cargo cargo) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(cargo);
		transaction.commit();
		session.close();
	}

	public void update(Cargo cargo) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(cargo);
		transaction.commit();
		session.close();
	}

	public void delete(Cargo cargo) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(cargo);
		transaction.commit();
		session.close();
	}

	public void consult(Cargo cargo) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		cargo = (Cargo) session.load(Cargo.class, new String("Cargo"));
	}

	public Cargo getCargo(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Cargo) session.load(Cargo.class, id);
	}
	
		
	public List<Cargo> list() 
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();
	String sql = "SELECT new Cargo( c.idCargo, c.descricao ) FROM Cargo c";
	List<Cargo> lista = session.createQuery(sql).list();
	transaction.commit();
	session.close();
	return lista;
	}
	
	

}

